package com.huikeportal.controller;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.huike.model.MsgResponse;
import com.huike.tools.DateTool;
import com.huike.tools.MessageTool;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.UserManagerInfo;
import com.huikeportal.model.common.InterFaceModel;
import com.huikeportal.service.CustomInfoService;
import com.huikeportal.service.InterFaceCommon;

@Controller
public class ChangePersonInfoAPI {

	@Autowired
	private InterFaceCommon interFaceCommon;

	@Autowired
	private CustomInfoService customInfoService;

	/**
	 * 更新个人资料
	 * 
	 * @param userId
	 * @param realName
	 * @param email
	 * @param phone
	 * @param tel
	 * @return
	 */
	@RequestMapping(value = "/huike_changePersonInfo", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String changePersonInfo(String userId, String realName,
			String email, String phone, String tel) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_changePersonInfo");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userId == null || realName == null || "".equals(userId)
				|| "".equals(realName)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		UserManagerInfo userManagerInfo = new UserManagerInfo();
		userManagerInfo.setUserId(userId);
		userManagerInfo.setEmail(email);
		userManagerInfo.setTel(tel);
		userManagerInfo.setPhone(phone);
		userManagerInfo.setRealName(realName);
		MessageCommon messageCommon;
		try {
			messageCommon = customInfoService.customSub(userManagerInfo);
			// 如果更新失败
			if (!messageCommon.isFlag()) {
				msgResponse.setRespCode("960001");
				msgResponse.setRespCodeDes(MessageTool.RESP_960001);
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				interFaceModel.setInterStatus("failure");
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			}
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		msgResponse.setRespCode("90001");
		msgResponse.setRespCodeDes(MessageTool.RESP_90001);
		interFaceModel.setEndTime(DateTool.getDateYmdHms());
		interFaceModel.setInterStatus("success");
		interFaceCommon.logInterface(interFaceModel);
		return JSONObject.fromObject(msgResponse).toString();
	}
}
